# =========================================================
# Appendix D — Conjoint Diagnostics (Table D1, Figures D1–D12)
# =========================================================

# ---- Packages ----
library(tidyverse)
library(haven)
library(stargazer)
library(sandwich)
library(lmtest)
library(dotwhisker)
library(ggeasy)
library(ggplot2)

# ---- Directories ----
base_dir       <- "~/Dropbox/Issue voting Chile"
data_dir       <- file.path(base_dir, "09_replication/data")         # merged files for balance
clean_data_dir <- file.path(base_dir, "01_data/clean_data")          # wave2_conjoint.dta
out_dir        <- file.path(base_dir, "09_replication/output")
dir.create(out_dir, showWarnings = FALSE, recursive = TRUE)

# =========================================================
# Table D1 — Balance on key covariates
# =========================================================

# -- Load merged data used for Table D1 --
d <- read_dta(file.path(data_dir, "wave2_conjoint_merged.dta"))

# -- Stata-like: tab sex_wave2, gen(sex_1) --
sx_mm <- model.matrix(~ as_factor(sex_wave2) - 1, d)
colnames(sx_mm) <- paste0("sex_1", seq_len(ncol(sx_mm)))
d <- bind_cols(d, as_tibble(sx_mm))

# -- Stata-like: tab education2, gen(college) --
ed_mm <- model.matrix(~ as_factor(education2) - 1, d)
colnames(ed_mm) <- paste0("college", seq_len(ncol(ed_mm)))
d <- bind_cols(d, as_tibble(ed_mm))

# -- Stata-like: encode nse_wave2 -> nse_enc (alphabetical on labels/strings) --
nse_chr <- if (inherits(d$nse_wave2, "haven_labelled")) {
  as.character(haven::as_factor(d$nse_wave2))
} else if (is.factor(d$nse_wave2)) {
  as.character(d$nse_wave2)
} else {
  as.character(d$nse_wave2)
}
levs_alpha <- sort(unique(nse_chr[!is.na(nse_chr)]))
d <- d %>% mutate(
  nse_enc      = as.integer(factor(nse_chr, levels = levs_alpha)),
  duration_min = duration_wave2 / 60
)

# -- Cluster-robust helpers (lm) --
cl_vcov <- function(model, cluster_var) {
  cl <- model.frame(model)[[cluster_var]]
  sandwich::vcovCL(model, cluster = cl)
}
robust_se_p <- function(model, cluster_var) {
  V <- cl_vcov(model, cluster_var)
  se <- sqrt(diag(V)); tval <- coef(model) / se
  pval <- 2 * pnorm(abs(tval), lower.tail = FALSE)
  list(se = se, p = pval)
}

# -- RHS + labels (atr1–atr6) --
f_rhs       <- ~ atr1 + atr2 + atr3 + atr4 + atr5 + atr6
keep_coefs  <- c("atr1","atr2","atr3","atr4","atr5","atr6")
nice_labels <- c("Ideology","Gender","Age","Feminism","Immigration","Crime")

# -- Fit models (cluster by numericalid_wave2) --
m1 <- lm(update(nse_enc      ~ 1, f_rhs), data = d)
m2 <- lm(update(college5     ~ 1, f_rhs), data = d)
m3 <- lm(update(age_wave2    ~ 1, f_rhs), data = d)
m4 <- lm(update(duration_min ~ 1, f_rhs), data = d)
m5 <- lm(update(sex_12       ~ 1, f_rhs), data = d)

mods    <- list(m1, m2, m3, m4, m5)
rp      <- lapply(mods, robust_se_p, cluster_var = "numericalid_wave2")
ses_lst <- lapply(rp, `[[`, "se")
p_lst   <- lapply(rp, `[[`, "p")

cat("\n================= Table D1. Balance Table Key Covariates =================\n")
stargazer(
  m1, m2, m3, m4, m5,
  type = "text",
  digits = 3,
  keep = keep_coefs,
  covariate.labels = nice_labels,
  omit = "Intercept",
  dep.var.labels = c("NSE (encoded)","College=cat5","Age","Duration (min)","Sex=cat2"),
  se = ses_lst, p = p_lst,
  add.lines = list(
    c("R-squared", sprintf("%.3f", sapply(mods, \(m) summary(m)$r.squared))),
    c("Obs.",      sapply(mods, nobs))
  )
)

# =========================================================
# Common setup for Figures D1–D12
# =========================================================

# -- Load base conjoint data for diagnostic figures --
wave2_conjoint <- read_dta(file.path(clean_data_dir, "wave2_conjoint.dta"))

# -- Attribute aliases (tidy names) --
atr <- c("a_1","a_2","a_3","a_4","a_5","a_6")
wave2_conjoint[, atr] <- lapply(wave2_conjoint[, atr], factor)
wave2_conjoint <- wave2_conjoint |>
  mutate(
    Ideology    = a_1,
    Gender      = a_2,
    Age         = a_3,
    Feminism    = a_4,
    Immigration = a_5,
    Crime       = a_6
  ) |>
  # rename posicion -> order (NOTE: 'order' is a base function; always backtick in formulas)
  mutate(order = posicion) |>
  # factor/recode for labels
  mutate(
    Ideology = fct_relevel(Ideology, "Right", "Left"),
    Feminism = fct_relevel(Feminism, "Non-Feminist", "Feminist"),
    Crime    = fct_recode(Crime,
                          `Less Punitive` = "No Harsher Punishment",
                          `More Punitive` = "Harsher Punishment"),
    Immigration = fct_recode(Immigration,
                             `New Restrictions` = "New Immigration Restrictions",
                             `No Restrictions`  = "No Restrictions")
  ) |>
  # create Days BEFORE subsetting, so subsets contain this column
  mutate(
    days  = as.numeric(format(endtime, "%d")),
    month = as.numeric(format(endtime, "%m"))
  )

# -- Subsamples used across figures (now include 'days' and 'order') --
left_fem    <- subset(wave2_conjoint, left_feminist == 1)
right_mig   <- subset(wave2_conjoint, right_anti_mig == 1)
left_crime  <- subset(wave2_conjoint, left  == 1 & (a6f %in% c("Muy de acuerdo","De acuerdo")))
right_crime <- subset(wave2_conjoint, right == 1 & (a6f %in% c("Muy de acuerdo","De acuerdo")))

# -- Shared styling helper (add with +) --
dwplot_style <- function(title,
                         xlim   = c(-0.2, 0.8),
                         breaks = c(-0.2, 0, 0.2, 0.4, 0.6, 0.8)) {
  list(
    theme_bw(),
    scale_color_manual(values = rep("black", 9)),
    guides(color = "none"),
    theme(
      axis.text.x = element_text(size = 10, color = "black"),
      axis.title.x = element_blank(),
      plot.title   = element_text(size = 11),
      axis.text.y  = element_text(size = 10)
    ),
    scale_x_continuous(limits = xlim, breaks = breaks),
    labs(title = title),
    easy_center_title(),
    geom_vline(xintercept = 0, color = "black", linetype = "dashed")
  )
}

# =========================================================
# ORDER interaction — Figures D1–D4
# =========================================================

# Figure D1 — Right & Anti-Immigration: Ideology*Order, Immigration*Order
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D1 <- lm(choice_clean2 ~ Ideology*`order` + Gender + Age + Feminism + Immigration*`order` + Crime,
           data = right_mig)
p_D1 <- dwplot(m_D1) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "order" = "Order",
    "IdeologyRight:order" = "Right*Order",
    "FeminismFeminist" = "Feminist",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "order:ImmigrationNew Restrictions" = "New Restrictions*Order",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Right and Anti-Immigration Subsample")
ggsave(file.path(out_dir, "D1_order_right_anti_migration.png"), p_D1, width = 6, height = 5)

# Figure D2 — Left & Feminist: Ideology*Order, Feminism*Order
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D2 <- lm(choice_clean2 ~ Ideology*`order` + Gender + Age + Feminism*`order` + Immigration + Crime,
           data = left_fem)
p_D2 <- dwplot(m_D2) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "order" = "Order",
    "IdeologyLeft:order" = "Left*Order",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Left and Feminist Subsample")
ggsave(file.path(out_dir, "D2_order_left_feminist.png"), p_D2, width = 6, height = 5)

# Figure D3 — Right & More Punitive: Ideology*Order, Crime*Order
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D3 <- lm(choice_clean2 ~ Ideology*`order` + Gender + Age + Feminism + Immigration + Crime*`order`,
           data = right_crime)
p_D3 <- dwplot(m_D3) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "order" = "Order",
    "IdeologyRight:order" = "Right*Order",
    "CrimeMore Punitive" = "More Punitive",
    "order:CrimeMore Punitive" = "More Punitive*Order",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Right and More Punitive Subsample")
ggsave(file.path(out_dir, "D3_order_right_more_punitive.png"), p_D3, width = 6, height = 5)

# Figure D4 — Left & More Punitive: Ideology*Order, Crime*Order
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D4 <- lm(choice_clean2 ~ Ideology*`order` + Gender + Age + Feminism + Immigration + Crime*`order`,
           data = left_crime)
p_D4 <- dwplot(m_D4) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "order" = "Order",
    "IdeologyLeft:order" = "Left*Order",
    "CrimeMore Punitive" = "More Punitive",
    "order:CrimeMore Punitive" = "More Punitive*Order",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Left and More Punitive Subsample")
ggsave(file.path(out_dir, "D4_order_left_more_punitive.png"), p_D4, width = 6, height = 5)

# =========================================================
# WAVE interaction — Figures D5–D8
# =========================================================

wave2_conjoint$set <- as.numeric(wave2_conjoint$set)

# Figure D5 — Right & Anti-Immigration: Ideology*Wave, Immigration*Wave
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D5 <- lm(choice_clean2 ~ Ideology*set + Gender + Age + Feminism + Immigration*set + Crime,
           data = right_mig)
p_D5 <- dwplot(m_D5) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "set" = "Wave",
    "IdeologyRight:set" = "Right*Wave",
    "FeminismFeminist" = "Feminist",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "set:ImmigrationNew Restrictions" = "New Restrictions*Wave",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Right and Anti-Immigration Subsample")
ggsave(file.path(out_dir, "D5_wave_right_anti_migration.png"), p_D5, width = 6, height = 5)

# Figure D6 — Left & Feminist: Ideology*Wave, Feminism*Wave
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D6 <- lm(choice_clean2 ~ Ideology*set + Gender + Age + Feminism*set + Immigration + Crime,
           data = left_fem)
p_D6 <- dwplot(m_D6) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "set" = "Wave",
    "IdeologyLeft:set" = "Left*Wave",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Left and Feminist Subsample")
ggsave(file.path(out_dir, "D6_wave_left_feminist.png"), p_D6, width = 6, height = 5)

# Figure D7 — Right & More Punitive: Ideology*Wave, Crime*Wave
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D7 <- lm(choice_clean2 ~ Ideology*set + Gender + Age + Feminism + Immigration + Crime*set,
           data = right_crime)
p_D7 <- dwplot(m_D7) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "set" = "Wave",
    "IdeologyRight:set" = "Right*Wave",
    "CrimeMore Punitive" = "More Punitive",
    "set:CrimeMore Punitive" = "More Punitive*Wave",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Right and More Punitive Subsample")
ggsave(file.path(out_dir, "D7_wave_right_more_punitive.png"), p_D7, width = 6, height = 5)

# Figure D8 — Left & More Punitive: Ideology*Wave, Crime*Wave
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D8 <- lm(choice_clean2 ~ Ideology*set + Gender + Age + Feminism + Immigration + Crime*set,
           data = left_crime)
p_D8 <- dwplot(m_D8) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "set" = "Wave",
    "IdeologyLeft:set" = "Left*Wave",
    "CrimeMore Punitive" = "More Punitive",
    "set:CrimeMore Punitive" = "More Punitive*Wave",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Left and More Punitive Subsample")
ggsave(file.path(out_dir, "D8_wave_left_more_punitive.png"), p_D8, width = 6, height = 5)

# =========================================================
# DAYS interaction — Figures D9–D12
# =========================================================

# (days already created BEFORE subsetting above, so it's present in all subsets)

# Figure D9 — Right & Anti-Immigration: Ideology*Days, Immigration*Days
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D9 <- lm(choice_clean2 ~ Ideology*days + Gender + Age + Feminism + Immigration*days + Crime,
           data = right_mig)
p_D9 <- dwplot(m_D9) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "days" = "Days",
    "IdeologyRight:days" = "Right*Days",
    "FeminismFeminist" = "Feminist",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "days:ImmigrationNew Restrictions" = "New Restrictions*Days",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Right and Anti-Immigration Subsample")
ggsave(file.path(out_dir, "D9_days_right_anti_migration.png"), p_D9, width = 6, height = 5)

# Figure D10 — Left & Feminist: Ideology*Days, Feminism*Days
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D10 <- lm(choice_clean2 ~ Ideology*days + Gender + Age + Feminism*days + Immigration + Crime,
            data = left_fem)
p_D10 <- dwplot(m_D10) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "days" = "Days",
    "IdeologyLeft:days" = "Left*Days",
    "FeminismFeminist" = "Feminist",
    "days:FeminismFeminist" = "Feminist*Days",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions",
    "CrimeMore Punitive" = "More Punitive"
  )) +
  dwplot_style("Left and Feminist Subsample")
ggsave(file.path(out_dir, "D10_days_left_feminist.png"), p_D10, width = 6, height = 5)

# Figure D11 — Right & More Punitive: Ideology*Days, Crime*Days
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Left","Right"))
m_D11 <- lm(choice_clean2 ~ Ideology*days + Gender + Age + Feminism + Immigration + Crime*days,
            data = right_crime)
p_D11 <- dwplot(m_D11) +
  scale_y_discrete(labels = c(
    "IdeologyRight" = "Right",
    "days" = "Days",
    "IdeologyRight:days" = "Right*Days",
    "CrimeMore Punitive" = "More Punitive",
    "days:CrimeMore Punitive" = "More Punitive*Days",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Right and More Punitive Subsample")
ggsave(file.path(out_dir, "D11_days_right_more_punitive.png"), p_D11, width = 6, height = 5)

# Figure D12 — Left & More Punitive: Ideology*Days, Crime*Days
wave2_conjoint$Ideology <- factor(wave2_conjoint$Ideology, levels = c("Right","Left"))
m_D12 <- lm(choice_clean2 ~ Ideology*days + Gender + Age + Feminism + Immigration + Crime*days,
            data = left_crime)
p_D12 <- dwplot(m_D12) +
  scale_y_discrete(labels = c(
    "IdeologyLeft" = "Left",
    "days" = "Days",
    "IdeologyLeft:days" = "Left*Days",
    "CrimeMore Punitive" = "More Punitive",
    "days:CrimeMore Punitive" = "More Punitive*Days",
    "FeminismFeminist" = "Feminist",
    "GenderWoman" = "Woman", "Age50" = "50", "Age60" = "60",
    "ImmigrationNew Restrictions" = "New Restrictions"
  )) +
  dwplot_style("Left and More Punitive Subsample")
ggsave(file.path(out_dir, "D12_days_left_more_punitive.png"), p_D12, width = 6, height = 5)

# =========================
# End Appendix D script
# =========================

